import pgobject
import sys_server
from message import *
from util import *

class PgContainer(pgobject.PgObject):
    def __init__(self, id = -1, background = Color.GRAY,
                 foreground = Color.BLACK, rect = (0,0,0,0)):
        pgobject.PgObject.__init__(self, id, background, foreground, rect)
        self.children = []

    def add_child(self, child):
        self.children.append(child)
        child.parent = self

    def remove_child(self, child):
        self.children.remove_child(child)
    
############################################################################
class __PgScreen(PgContainer, Observer):
    def __init__(self, id = -1, background = Color.GRAY,
                 foreground = Color.BLACK, rect = (100,100,200,200)):
        PgContainer.__init__(self, id, background, foreground, rect)
        sys_server.set_msg_observer(MSG_KEY, self)
        sys_server.set_msg_observer(MSG_LBUTTONDOWN, self)
        sys_server.set_msg_observer(MSG_LBUTTONUP, self)
        sys_server.set_msg_observer(MSG_RBUTTONDOWN, self)
        sys_server.set_msg_observer(MSG_RBUTTONUP, self)
        sys_server.set_msg_observer(MSG_DRAW, self)
        sys_server.set_msg_observer(MSG_MOUSEMOVE, self)

    def add_window(self, win):
        self.add_child(win)
        
    def sys_message(self, msg):
        print '----', msg.type
        if msg.type == MSG_KEY:
            self.children[-1].message(msg)
        elif msg.type == MSG_LBUTTONDOWN or msg.type == MSG_LBUTTONUP or\
                msg.type == MSG_RBUTTONDOWN or msg.type == MSG_RBUTTONUP:
            if self.is_contain(msg.value):
                self.children[-1].message(msg)
        elif msg.type == MSG_DRAW:
            self.children[-1].message(msg)
        elif msg.type == MSG_MOUSEMOVE:
            if self.is_contain((msg.value[0], msg.value[1])):
                self.children[-1].message(msg)
        else:
            raise 'unknown message type'

#############################################################################
class PgWindow(PgContainer):
    def __init__(self, title = '', id = -1, background = Color.GRAY,
                 foreground = Color.BLACK, rect = (0,0,0,0)):
        PgContainer.__init__(self, id, background, foreground, rect)
        self.title = title
        self.focus_obj = None
        self.text = []

    def add_widget(self, wid):
        self.add_child(wid)
        if self.focus_obj:
            self.focus_obj.is_focused = False
        self.focus_obj = wid
        self.focus_obj.is_focused = True
        
    def change_focus(self, newobj):
        if self.focus_obj != newobj:
            self.focus_obj.message(Msg(MSG_LOSE_FOCUS))
            newobj.message(Msg(MSG_GET_FOCUS))
            self.focus_obj.is_focused = False
            self.focus_obj = newobj
            self.focus_obj.is_focused = True

    def on_key(self, msg):
        print "PgWindow : key"
        if self.focus_obj:
            self.focus_obj.message(msg)
        
    def on_lbuttondown(self, msg):
        print 'PgWindow : lbuttondown', msg.value
        for c in self.children[::-1]:
            if c.is_contain(msg.value):
                self.change_focus(c)
                c.message(msg)
                return

    def on_lbuttonup(self, msg):
        print 'PgWindow : lbuttonup', msg.value
        for c in self.children[::-1]:
            if c.is_contain(msg.value):
                self.change_focus(c)
                c.message(msg)
                return

    def on_rbuttondown(self, msg):
        print 'PgWindow : rbuttondown', msg.value
        for c in self.children[::-1]:
            if c.is_contain(msg.value):
                self.change_focus(c)
                c.message(msg)
                return

    def on_rbuttonup(self, msg):
        print 'PgWindow : rbuttonup', msg.value
        for c in self.children[::-1]:
            if c.is_contain(msg.value):
                self.change_focus(c)
                c.message(msg)
                return

    def on_draw(self, canvas):
        print 'PgWindow : draw'
        self.draw_client(canvas)
        x,y,w,h = self.rect
        canvas.fill_rect((x,y+h-15,w,15),Color.BLUE)
        canvas.draw_text(self.title, (x+w)/2-10, y+h-14)
        ny = y+h-10
        for t in self.text:
            canvas.draw_text(t, x = 10, y = ny, size = 20, color = (255,0,0,255))
            ny = ny - 25
        for c in self.children:
            msg = Msg(MSG_DRAW)
            c.message(msg)

    def on_mousemove(self, msg):
        for c in self.children[:]:
            c.message(msg)
            
    def on_OK(self, msg):
        print 'PgWindow : Button OK'
        self.text.append('OK Pressed~')

    def on_CANCEL(self, msg):
        print 'PgWindow : Button Cancel'
        self.text.append('Cancel Pressed~')
        
    msg_map = {MSG_KEY   : on_key,
               MSG_DRAW  : on_draw,
               MSG_LBUTTONDOWN : on_lbuttondown,
               MSG_LBUTTONUP : on_lbuttonup,
               MSG_RBUTTONDOWN : on_rbuttondown,
               MSG_RBUTTONUP : on_rbuttonup,
               MSG_MOUSEMOVE : on_mousemove,
               MSG_NOTIFY:
                   {1 : on_OK,
                    2 : on_CANCEL}}
    
#############
__screen = __PgScreen(rect = (0,0,800,800))

def get_screen():
    return __screen

#############    
if __name__ == '__main__':
    scr = get_screen()
    win = PgWindow()
    scr.add_child(win)
    sys_server.run()
